package com.taotao.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taotao.mapper.EyGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.enums.OperateType;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.DateUtil;
import com.taotao.mapper.EyCollectMapper;
import com.taotao.mapper.EyMemberMapper;
import com.taotao.pojo.EyCollect;
import com.taotao.pojo.EyCollectExample;
import com.taotao.pojo.EyCollectExample.Criteria;
import com.taotao.pojo.EyGoods;
import com.taotao.pojo.EyMember;
import com.taotao.service.CollectService;
import com.taotao.service.OperateLogService;

@Service
public class CollectServiceImpl implements CollectService {

	@Autowired
	EyCollectMapper eyCollectMapper;
	@Autowired
	EyGoodsMapper eyGoodsMapper;
	@Autowired
	EyMemberMapper eyMemberMapper;
	@Autowired
	OperateLogService operateLogService;
	
	BaseResult baseResult = new BaseResult();
	

	@Override
	public Map<String, Object> goodsCollection(Map<String, String> map) {
		try {
			String goodsId=map.get("goodsId");
			String memberId=map.get("memberId");
			String isfav=map.get("isfav");
			System.out.println("goodsId:"+goodsId+" memberId"+memberId+"isfav:"+isfav);
			EyGoods goods = eyGoodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
			
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			member.setUpdateTime(DateUtil.getDateAndTime());
			
			String operateType="";
			if("true".equals(isfav)){//收藏	
				if(goods.getCollectNum()==null){
					goods.setCollectNum(1);
				}
				goods.setCollectNum(goods.getCollectNum()+1);
				eyGoodsMapper.updateByPrimaryKey(goods);
				
				EyCollect c=new EyCollect();
				c.setGoodsId(Integer.parseInt(goodsId));
				c.setMemberId(Integer.parseInt(memberId));
				c.setCreateTime(DateUtil.getDateAndTime());
				eyCollectMapper.insert(c);
				
				if(member.getCollectNum()==null){
					member.setCollectNum(0);
				}
				member.setCollectNum(member.getCollectNum()+1);
				operateType=OperateType.operateType_15.getCode();
			}else if("false".equals(isfav)){ //取消收藏
				Integer collectNum = goods.getCollectNum();
				if(collectNum>0){
					goods.setCollectNum(goods.getCollectNum()-1);
					eyGoodsMapper.updateByPrimaryKey(goods);
				}
				
				EyCollectExample example = new EyCollectExample();
				Criteria criteria = example.createCriteria();
				criteria.andGoodsIdEqualTo(Integer.parseInt(goodsId));
				criteria.andMemberIdEqualTo(Integer.parseInt(memberId));
				eyCollectMapper.deleteByExample(example);
				
				if(member.getCollectNum()>0){  //收藏数量-1
					member.setCollectNum(member.getCollectNum()-1);
				}
				operateType=OperateType.operateType_16.getCode();
			}
			eyMemberMapper.updateByPrimaryKeySelective(member);
			
			operateLogService.insert(Integer.parseInt(memberId), null,Integer.parseInt(goodsId),"操作收藏",operateType,""); 
			
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorJsonObj("网络繁忙，请稍后再试");
		}
	}


	@Override
	public Map<String, Object> isColleced(Map<String, String> map) {
		try {
			String goodsId=map.get("goodsId");
			String memberId=map.get("memberId");
			EyCollectExample example = new EyCollectExample();
			Criteria criteria = example.createCriteria();
			criteria.andGoodsIdEqualTo(Integer.parseInt(goodsId));
			criteria.andMemberIdEqualTo(Integer.parseInt(memberId));
			List<EyCollect> list = eyCollectMapper.selectByExample(example);
			Map<String,Object> returnMap=new HashMap<String,Object>();
			if(list!=null && list.size()>0){
				returnMap.put("isColleced", true);
			}else{
				returnMap.put("isColleced", false);
			}
			return baseResult.getSuccMap(returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorJsonObj("网络繁忙，请稍后再试");
		}
	}
}
